import 'package:cookie_jar/cookie_jar.dart';
import 'package:path_provider/path_provider.dart';


import '../services/utils.dart';

class CookieService {
  static final CookieService instance = CookieService._internal();
  late final PersistCookieJar cookieJar;
  late final Future<void> _initialization;

  factory CookieService() => instance;

  CookieService._internal() {
    // 启动异步初始化
    myLog("CookieJar初始化开始");
    _initialization = _initPersistentCookieJar();
    myLog("CookieJar初始化完成");
  }

  // 初始化持久化 CookieJar
  Future<void> _initPersistentCookieJar() async {
    final appDocDir = await getApplicationDocumentsDirectory();
    final appDocPath = appDocDir.path;
    cookieJar = PersistCookieJar(
      storage: FileStorage("$appDocPath/.cookies/"),
      ignoreExpires: true, // 保持与原始配置一致
    );
  }

  // 确保初始化完成的公共方法
  Future<void> ensureInitialized() => _initialization;
}